<html>

<head>
<script type="text/javascript" src="js/chosen.jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="media/css/chosen.min.css">
<style type="text/css">
.chosen-container-active{
    z-index:9999 !important;;
}
.chosen-container{
	z-index:9999 !important;   
}
.ui-dialog.add-edit-form_inner-class {
  position: fixed;
}
</style>
	<script type="text/javascript">
		var aJaxURL		= "server-side/view/catalog.action.php";		//server side folder url
		var upJaxURL	= "server-side/upload/file.action.php";			//server side folder url
		var tName		= "example";									//table name
		var fName		= "add-edit-form";								//form name
		var img_name	= "0.jpg";
		
		$(document).ready(function (){
			$('.ui-layout-toggler-north').click(function(){
			    //alert("OK");
			    $('#chosen_select').toggleClass('hide');
			});
			LoadTable();
			
			/* Add Button ID, Delete Button ID */
			GetButtons("add_button", "delete_button", "", "", "clear_button");
			
			SetEvents("add_button", "delete_button", "check-all", tName, fName, aJaxURL);
		});
        
		function LoadTable(tname){
			/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
			if (tname=="inner_table") {
				GetDataTable("inner_table", aJaxURL, "get_list1&id="+ID, 4, "", 0, "");
				return 0;}
			GetDataTable(tName, aJaxURL, "get_list", 6, "", 0, "");
		}
		
		function LoadDialog(fname){
			ID=$("#prod_id").val();
			if(fname=="add-edit-form_inner"){				
				var buttons = {
			            "save-dialog-inner": {
			                text: "შენახვა ",
			                id: "save-dialog-inner",
			                click: function () {
			                	param={ act               :'save_identiti',
			    						printer_model     : $("#printer_model").val(),
			    						part_type         : $("#part_type").val(),
			    						category          : $("#category").val(),
			    						amount			  : $("#amount").val(), 
			    						prod_inner_id     : $("#prod_inner_id").val(), 						
			    						id                :   ID			    
			    						}
	    						if(param.printer_model<1){ alert ("პრინტერის მოდელი არასწორია"); return 0;}
			                	if(param.part_type<1){ alert ("ნაწილის ტიპი არასწორია"); return 0;}
			                	if(param.category<1){ alert ("ნაწილის კატეგორია არასწორია"); return 0;}
			                	if(param.category==0){ alert ("რაოდენობა არასწორია"); return 0;}
			                	if(param.id<1){ alert ("შეცდომა"); return 0;}
				       			 $.ajax({
					 			        url: aJaxURL,
					 				    data: param,
					 			        success: function(data) {
					 						if(typeof(data.error) != "undefined"){
					 							if(data.error != ""){
					 								alert(data.error);
					 							}else{
					 								GetDataTable("inner_table", aJaxURL, "get_list1&id="+ID, 4, "", 0, "");
					 				        		CloseDialog("add-edit-form_inner");
					 							}
					 						}
					 				    }
					 			    });
				                
			                }
			            },
			            "cancel": {
			                text: "დახურვა",
			                id: "cancel-dialog-inner",
			                click: function () {
			                	$(this).dialog("close");
			                }
						}
					};
				
				GetDialog(fname, 450, "auto", buttons);
				$('#printer_model, #part_type, #printer_category').chosen({ search_contains: true });				
				return 0};
			var id		= $("#prod_id").val();
			
			if(empty($("#page_resource").val()))
				$("#page_resource").val(0);
			
			if(empty($("#job_circle").val()))
				$("#job_circle").val(0)
			
			if(id == ""){
				$("#min_amount").val(1);
			}
			
			$("#choose_button").button({
	            icons: {
	                primary: "ui-icon-arrowreturnthick-1-n"
	            }
        	});
        	
			$("#upload_button").button({
	            icons: {
	                primary: "ui-icon-arrowreturnthick-1-n"
	            }
        	});

			var img_url	= $("#upload_img").attr("src"); 
	    	img_name	= img_url.split("\/")[4]; //Get image name element 4
	    	if(img_name != "0.jpg"){
	    		$("#choose_button").button("disable");
	    	}
        	
			/* Dialog Form Selector Name, Buttons Array */
			GetDialog(fName, 790, "auto", "");
			GetButtons("add_button_inner", "delete_button_inner", "", "", "");
			GetDataTable("inner_table", aJaxURL, "get_list1&id="+ID, 4, "", 0, "");
	        SetEvents("add_button_inner", "delete_button_inner", "check-all", 'inner_table', "add-edit-form_inner", aJaxURL+"?inner=true");
			
			
		}
				
	    // Add - Save
	    $(document).on("click", "#save-dialog", function () {
	    	param = new Object();

            //Action
	    	param.act	= "save_prod";

            //Main
	    	param.id	= $("#prod_id").val();

	    	param.cod	= $("#code").val().trim();
	    	param.pn	= $("#part_number").val().trim();
	    	param.n		= $("#name").val();
	    	
	    	param.cat 	= $("#category").val();
	    	param.un 	= $("#unit").val();

	    	param.cy 	= $("#country").val();
	    	param.pr 	= $("#page_resource").val();
	    	param.jc 	= $("#job_circle").val();
	    	param.min	= $("#min_amount").val().trim();

		    param.c		= $("#comment").val();
		    
            //Image
		    param.img 	= img_name;
			
			if(param.cod == ""){
				alert("შეავსეთ პროდუქტის კოდი!");
			}else if(param.n == ""){
				alert("შეავსეთ პროდუქტის დასახელება!");
			}else if(param.cat == 0){
				alert("შეავსეთ კატეგორია!");
			}else if(param.un == 0){
				alert("შეავსეთ ზომის ერთეული!");				
			}else {
			    $.ajax({
			        url: aJaxURL,
				    data: param,
			        success: function(data) {
				        if(typeof(data.error) != "undefined"){
							if(data.error != ""){
								alert(data.error);
							}else{
								LoadTable();
					        	CloseDialog(fName);
							}
						}
				    }
			    });
			}
		});
		
	    $(document).on("click", "#choose_button", function () {
		    $("#choose_file").click();
		});
		
	    $(document).on("change", "#choose_file", function () {
	    	var file		= $(this).val();	    
		    var name		= uniqid();
		    var path		= "../../media/uploads/images/production/";
		    
		    var ext = file.split('.').pop().toLowerCase();
	        if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) { //echeck file type
	        	alert('This is not an allowed file type.');
                this.value = '';
	        }else{
	        	img_name = name + "." + ext;
	        	$("#choose_button").button("disable");
	        	$.ajaxFileUpload({
	    			url: upJaxURL,
	    			secureuri: false,
	    			fileElementId: "choose_file",
	    			dataType: 'json',
	    			data:{
						act: "upload_file",
						path: path,
						file_name: name,
						type: ext
					},
	    			success: function (data, status){
	    				if(typeof(data.error) != 'undefined'){
    						if(data.error != ''){
    							alert(data.error);
    						}else{
    							$("#upload_img").attr("src", "media/uploads/images/production/" + img_name);
    						}
    					}
    				},
    				error: function (data, status, e)
    				{
    					alert(e);
    				}
    			});
	        }
		});
		
	    $(document).on("click", "#view_image", function () {
		    var src = $("#upload_img").attr("src");
		    $("#view_img").attr("src", src);
			var buttons = {
				"cancel": {
		            text: "დახურვა",
		            id: "cancel-dialog",
		            click: function () {
		                $(this).dialog("close");
		            }
		        }
		    };
	    	GetDialog("image-form", "auto", "auto", buttons);
		});
		
	    $(document).on("click", "#upload_img", function () {
		    var src = $("#upload_img").attr("src");
		    $("#view_img").attr("src", src);
			var buttons = {
				"cancel": {
		            text: "დახურვა",
		            id: "cancel-dialog",
		            click: function () {
		                $(this).dialog("close");
		            }
		        }
		    };
	    	GetDialog("image-form", "auto", "auto", buttons);
		});
		
	    $(document).on("click", "#delete_image", function () {
	    	var img_url	= $("#upload_img").attr("src");
	    	img_name	= img_url.split("\/")[4];	//Get image name element 4
	    	if(img_name != "0.jpg"){
		    	param = new Object();
		    	
	            //Action
		    	param.act		= "delete_file";
		    	
		    	param.path	 	= "../../media/uploads/images/production/";
			    param.file_name	= img_name;
			    var id			= $("#prod_id").val();
			    
	            $.ajax({
	                url: upJaxURL,
	                data: param,
	                success: function(data) {
	                    if (typeof(data.error) != "undefined") {
	                        if (data.error != "") {
	                            alert(data.error);
	                        } else {
	                        	$("#choose_button").button("enable");
	                        	$("#upload_img").attr("src", "media/uploads/images/production/0.jpg");
	                        	if(!empty(id)){
	                        		DeleteImage(id);
		                        }
	                        }
	                    }
	                }
	            });
			}
		});
		
	    function DeleteImage(prod_id) {
            $.ajax({
                url: aJaxURL,
                data: "act=delete_image&id=" + prod_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                    	if (data.error != "") {
                            alert(data.error);
                        } else{
                        	img_name = "0.jpg";                        	
                        }
                    }
                }
            });
        }
		
	    $(document).on("click", "#clear_button", function () {
	    	param = new Object();
	    	
			//Action
	    	param.act	= "get_file_list";
			param.path	= "../../media/uploads/images/production/";
			var file_list = "";
            $.ajax({
                url: upJaxURL,
                data: param,
                async: false, //r-value
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                        	file_list = data.file_list;
                        }
                    }
                }
            });
            	    	
            //Action
	    	param.act	= "clear";	    	
	    	param.file	= file_list;
	    	
	    	$.ajax({
                url: aJaxURL,
                data: param,
                async: false, //r-value
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            alert("Database Clear Successfully Completed");
                            file_list = data.file_list;
                        }
                    }
                }
            });
	    	file_list = $.parseJSON(file_list);
            
	    	for (var i = 0; i < file_list.length; i++) {		    	
		    	param.act		= "delete_file";		    	
			    param.file_name	= file_list[i];
			    			    
	            $.ajax({
	                url: upJaxURL,
	                data: param,
	                success: function(data) {
	                    if (typeof(data.error) != "undefined") {
	                        if (data.error != "") {
	                            alert(data.error);
	                        }
	                    }
	                }
	            });
		    }
		});
        
		$(document).on("keydown", "#code", function (event) {
			if (event.keyCode == $.ui.keyCode.ENTER){
            	$("#part_number").focus();
				event.preventDefault();					//Clear Dialog Default <b>keypress<b> Event
            }
		});
    </script>
</head>

<body>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container">
            <div id="dynamic">
            	<h2 align="center">პროდუქტები</h2>
	        	<div id="button_area">
	        		<button id="add_button">დამატება</button><button id="delete_button">წაშლა</button>
	        	</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th class="min">კოდი</th>
                            <th style="width: 368px">დასახელება</th>
                            <th class="min">ზომის ერთეული</th>
                            <th class="min">კატეგორია</th>
                            <th class="min">შენიშვნა</th>
                            <th class="check">#</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_code" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_name" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_size" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_comment" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                            	<input type="checkbox" name="check-all" id="check-all">
                            </th>
                        </tr>
                    </thead>
                </table>
	        	<div id="bottom_button_area">
	        		<button id="clear_button" class="right">clear</button>
	        	</div>
            </div>
            <div class="spacer">
            </div>
        </div>
    </div>
    
    <!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="პროდუქტები">
    	<!-- aJax -->
	</div>
	    <!-- jQuery Dialog -->
    <div id="add-edit-form_inner" class="form-dialog" title="პროდუქტები">
    	<!-- aJax -->
	</div>
	
    <!-- jQuery Dialog -->
    <div id="image-form" class="form-dialog" title="პროდუქციის სურათი">
    	<img id="view_img" src="media/uploads/images/production/0.jpg">
	</div>
</body>
</html>